﻿package com.tools
{
	import com.tools.CustomDispatcher;
	
	import com.notifications.XmlEvent;
	
	import flash.net.*;
	
	import flash.events.*;
	
	public class XmlLoader extends CustomDispatcher
	{
		private var _loader : URLLoader;
		
		function XmlLoader( xmlUrl : String )
		{
			_loader 			= new URLLoader( );
			_loader.dataFormat 	= URLLoaderDataFormat.TEXT;
			
			_loader.addEventListener ( 	Event.COMPLETE,
									  	loadingComplete );
			
			_loader.addEventListener ( 	HTTPStatusEvent.HTTP_STATUS,
									  	codeHTTP );
			
			_loader.addEventListener ( 	IOErrorEvent.IO_ERROR,
									  	loadingFailed );
			
			_loader.load( new URLRequest( xmlUrl ) );
		}
		
		private function loadingComplete( pEvt : Event ) : void
		{
			var donneesXML:XML = new XML ( pEvt.target.data );
			dispatchEvent( new XmlEvent( XmlEvent.XML_LOADED, true, false, donneesXML ) );
		}
		
		private function codeHTTP( pEvt : HTTPStatusEvent ) : void
		{
			
		}
		
		private function loadingFailed ( pEvt : IOErrorEvent ) : void
		{
			dispatchEvent( new XmlEvent( XmlEvent.XML_FAILED, true, false ) );
		}
	}
}